<?php

namespace App\Http\Controllers\API;

use App\Http\Resources\CaseInfoResource;
use App\Model\CaseInfo;
use App\Model\CaseStatistics;
use App\Model\InvestigationAgreement;
use App\Model\MediationRecord;
use App\Model\ReturnVisitRecord;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CaseController extends Controller
{

    public function index(Request $request)
    {
        $keyword = $request->get('keyword', '');
        $models = CaseInfo::query()->when($keyword, function ($query) use ($keyword) {
            $query->where('case_source', 'like', '%' . $keyword . '%');
        })->paginate();
        return CaseInfoResource::collection($models);
    }


    /**
     * 添加实例
     * @param Request $request
     * @return mixed
     */
    public function store(Request $request)
    {
        try{
            $model = new CaseInfo();
            $model->fill($request->all());
            $model->save();
            $this->checkCode($model->users_id,$model->case_type);
            return new CaseInfoResource($model);
        }catch (\Exception $e){
            dd($e);
        }
    }


    /**
     * 修改实例
     * @param $id
     * @param Request $request
     * @return mixed
     */
    public function update($id, Request $request)
    {
        try{
            $model = $this->findModel($id);
            $model->fill($request->all());
            $model->save();
            $this->checkCode($model->users_id,$model->case_type);
            return new CaseInfoResource($model);
        }catch (\Exception $e){
            dd($e);
        }


    }

    public function checkCode($users_id,$type){
        $num = CaseInfo::query()->where(['users_id'=>$users_id,'case_type'=>$type])->count();
        $quality = CaseInfo::query()->where(['users_id'=>$users_id,'case_type'=>$type])->avg('code');

        $statistic = CaseStatistics::query()->where(['users_id'=>$users_id,'case_type'=>$type])->first();
        if(!$statistic){
            $statistic = new CaseStatistics();
        }
//        dd(ceil($quality));
        $statistic->users_id = $users_id;
        $statistic->case_type = $type;
        $statistic->case_num = $num;
        $statistic->case_quality = intval($quality);
        $statistic->save();
        return true;
    }


    /**
     * 删除
     * @param $id
     * @return mixed
     */
    public function destroy($id)
    {
        $model = $this->findModel($id);
        try {
            $user_id = $model->users_id;
            $type = $model->case_type;
            $model->delete();
            InvestigationAgreement::query()->where('case_id',$id)->delete();
            MediationRecord::query()->where('case_id',$id)->delete();
            ReturnVisitRecord::query()->where('case_id',$id)->delete();
            $this->checkCode($user_id,$type);
            return response()->json(['state' => 'success']);

        } catch (\Exception $e) {
            return response()->json(['state' => 'failed', 'message' => $e->getMessage()], 406);
        }
    }


    /**
     * 返回单个实例
     * @param User $user
     * @return mixed
     */
    public function show($id)
    {
        $model = $this->findModel($id);
        return new CaseInfoResource($model);
    }


    /**
     * @param $id
     * @return Staff
     */
    public function findModel($id)
    {
        $model = CaseInfo::query()->where('id', $id)->first();
        return $model;
    }
}
